package com.bilibili.juc.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author zuife
 * @description
 * @since 2024-04-20 16:48:35
 */
public class CompletableFutureAPI3Demo {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        CompletableFuture.supplyAsync(() -> {
            //暂停3秒钟
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 1;
        },threadPool).handle((v,e) -> {
            System.out.println("22");
            return v + 2;
        }).handle((v,e) -> {
            int i=10/0;
            System.out.println("33");
            return v + 3;
        }).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println("结果：" + v);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            System.out.println("异常：" + e.getCause().getMessage());
            return null;
        });
        System.out.println(Thread.currentThread().getName()+ "main...先去忙其他");
        threadPool.shutdown();
    }
}
